package com.iuie.ns.datum.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iuie.ns.datum.service.IDevTransferService;
import com.iuie.ns.datum.service.ITableFieldService;
import com.iuie.ns.datum.service.ITableStructureService;
import com.iuie.ns.datum.vo.DevTransferResultVO;
import com.iuie.ns.datum.vo.TableChangeHistoryVO;
import com.iuie.ns.datum.vo.TableFieldVO;
import com.iuie.ns.datum.vo.TableStructureVO;
import com.iuie.ns.devtools.to.DataSetDevHeaderTO;
import com.iuie.ns.devtools.to.JavaBeanDevHeaderTO;
import com.iuie.ns.devtools.to.ResultMapDevHeaderTO;
import com.iuie.ns.devtools.to.SQLDevHeaderTO;
import com.iuie.ns.frame.web.dataset.vo.Page;
import com.iuie.ns.frame.web.response.vo.ResponseInfo;

/**
 * @desc 开发支撑模块控制器
 * @createTime 2018-01-24 10:05:00
 * @author iuie
 */
@RequestMapping("datum/development")
@Controller
public class DevelopmentController {
	
	@Resource
	private ITableStructureService tableStructureService;
	
	@Resource
	private ITableFieldService tableFieldService;
	
	@Resource
	private IDevTransferService transferService;
	
	/**
	 * 开发工具页面index
	 * @param request
	 * @return
	 * @date 2017年8月22日 上午8:43:57
	 */
	@RequestMapping
	public String index(HttpServletRequest request){
		return "view-datum/development/developmentTool";
	}

	/**
	 * 新增表实例页面-index
	 * @param request
	 * @return
	 * @date 2018-01-28 15:59:06
	 */
	@RequestMapping("addOne/index")
    public String addOneTableIndex(HttpServletRequest request) {
		request.setAttribute("newTableId", System.currentTimeMillis());
    	return "view-datum/development/addTable";
    }
	
	/**
	 * 修改表实例页面-index
	 * @param tableId
	 * @param request
	 * @return
	 */
	@RequestMapping("editOne/{tableId}/index")
    public String editOneTableIndex(@PathVariable("tableId") Long tableId, HttpServletRequest request) {
		request.setAttribute("tableId", tableId);
    	return "view-datum/development/addTable";
    }
	
	/**
	 * 批量新增数据库字段-index
	 * @param tableId
	 * @param request
	 * @return
	 * @date 2018年4月1日下午9:42:08
	 */
	@RequestMapping("field/batchAdd/{tableId}/index")
    public String batchAddFieldIndex(@PathVariable("tableId") Long tableId, HttpServletRequest request) {
		request.setAttribute("tableId", tableId);
    	return "view-datum/development/batchAddField";
    }
	
	/**
	 * 新增字段页面-index
	 * @param tableId
	 * @param request
	 * @return
	 * @date 2018-01-28 15:59:06
	 */
	@RequestMapping("addField/{tableId}/index")
    public String addOneFieldIndex(@PathVariable("tableId") Long tableId, HttpServletRequest request) {
		request.setAttribute("tableId", tableId);
    	return "view-datum/development/addField";
    }
	
	/**
	 * 修改字段页面-index
	 * @param tableId
	 * @param fieldId
	 * @param request
	 * @return
	 */
	@RequestMapping("editField/{tableId}/{fieldId}/index")
    public String editOneFieldIndex(@PathVariable("tableId") Long tableId, @PathVariable("fieldId") Long fieldId, HttpServletRequest request) {
		request.setAttribute("id", fieldId);
		request.setAttribute("tableId", tableId);
    	return "view-datum/development/addField";
	}
	
	/**
	 * 查看变更历史
	 * @param tableId
	 * @param request
	 * @return
	 */
	@RequestMapping("viewHistory/{tableId}/index")
    public String viewHistoryIndex(@PathVariable("tableId") Long tableId, HttpServletRequest request) {
		request.setAttribute("tableId", tableId);
		return "view-datum/development/viewHistory";
	}
    		
	
    /**
     * 分页查表实例
     * @param page
     * @param condition 查询条件
     * @return
     */
    @RequestMapping("find/page")
    @ResponseBody
    public ResponseInfo findTableByPage(Page page, TableStructureVO condition) {
        List<TableStructureVO> list = tableStructureService.findByPage(page, condition);
        return ResponseInfo.successInfo(list, page);
    }
    
    /**
     * 批量新增数据库字段成功
     * @param text
     * @param rule
     * @param tableId
     * @return
     * @date 2018年4月1日下午9:55:02
     */
    @RequestMapping(value = "field/batchAdd/{tableId}", method=RequestMethod.POST)
    @ResponseBody
    public ResponseInfo batchAddField(String text, String rule, @PathVariable("tableId") Long tableId) {
        tableFieldService.batchAddField(text, rule, tableId);
        return ResponseInfo.successInfo("批量新增字段成功！");
    }
    
    /**
     * 根据ID查询实例
     * @param page
     * @param condition 查询条件
     * @return
     */
    @RequestMapping("find/id/{id}")
    @ResponseBody
    public ResponseInfo findTableById(@PathVariable("id") Long id) {
        TableStructureVO tableStructure = tableStructureService.findById(id);
        return ResponseInfo.successInfoByObject(tableStructure);
    }
    
    /**
     * 根据tableId查询表的字段列表
     * @param id
     * @return
     */
    @RequestMapping("find/fields/tableId")
    @ResponseBody
    public ResponseInfo findFieldsByTableId(Long tableId) {
        List<TableFieldVO> list = tableFieldService.findFieldsByTableId(tableId);
        return ResponseInfo.successInfoByObject(list);
    }
    
    /**
     * 根据表实例ID删除表
     * @param id
     * @return
     */
    @RequestMapping("delete/id/{id}")
    @ResponseBody
    public ResponseInfo deleteTableById(@PathVariable("id") Long id) {
    	tableStructureService.deleteTableStructure(id);
    	return ResponseInfo.successInfo("删除成功！");
    }
    
    /**
     * 根据字段ID
     * @param id
     * @return
     */
    @RequestMapping("delete/field/{id}")
    @ResponseBody
    public ResponseInfo deleteFieldById(@PathVariable("id") Long id) {
    	tableFieldService.deleteById(id);
    	return ResponseInfo.successInfo("删除成功！");
    }
    
    /**
     * 新增和修改表实例
     * @param table
     * @param updateRemark
     * @return
     */
    @RequestMapping("save/table")
    @ResponseBody
    public ResponseInfo saveTableStructure(TableStructureVO table, String updateRemark) {
    	tableStructureService.saveTableStructure(table, updateRemark);
    	return ResponseInfo.successInfo("保存成功！");
    }
    
    /**
     * 新增和修改表字段
     * @param table
     * @param updateRemark
     * @return
     */
    @RequestMapping("save/field")
    @ResponseBody
    public ResponseInfo saveTableField(TableFieldVO field, String updateRemark) {
    	tableFieldService.saveTableField(field, updateRemark);
    	return ResponseInfo.successInfo("保存成功！");
    }
    
    /**
     * 更新是否用于查询的状态
     * @param fieldId
     * @param doQuery
     * @return
     */
    @RequestMapping("update/doQuery/{fieldId}/{doQuery}")
    @ResponseBody
    public ResponseInfo updateDoQuery(@PathVariable("fieldId") Long fieldId, @PathVariable("doQuery") String doQuery) {
    	tableFieldService.updateDoQuery(fieldId, doQuery);
    	return ResponseInfo.successInfo("更新[用于查询]属性成功！");
    }
    
    /**
     * 更新查询模式
     * @param fieldId
     * @param queryMode
     * @return
     */
    @RequestMapping("update/queryMode/{fieldId}/{queryMode}")
    @ResponseBody
    public ResponseInfo updateQueryMode(@PathVariable("fieldId") Long fieldId, @PathVariable("queryMode") String queryMode) {
    	tableFieldService.updateQueryMode(fieldId, queryMode);
    	return ResponseInfo.successInfo("更新[查询模式]属性成功！");
    }
    
    /**
     * 更新doUpdate的值
     * @param fieldId
     * @param doUpdate
     * @return
     * @date 2018年5月30日上午8:40:35
     */
    @RequestMapping("update/doUpdate/{fieldId}/{doUpdate}")
    @ResponseBody
    public ResponseInfo updateDoUpdate(@PathVariable("fieldId") Long fieldId, @PathVariable("doUpdate") String doUpdate) {
    	tableFieldService.updateDoUpdate(fieldId, doUpdate);
    	return ResponseInfo.successInfo("更新[用于更新]属性成功！");
    }
    
    /**
     * 更新“非空”属性
     * @param fieldId
     * @param allowNull
     * @return
     * @date 2018年5月30日上午8:41:39
     */
    @RequestMapping("update/allowNull/{fieldId}/{allowNull}")
    @ResponseBody
    public ResponseInfo updateAllowNull(@PathVariable("fieldId") Long fieldId, @PathVariable("allowNull") String allowNull) {
    	tableFieldService.updateAllowNull(fieldId, allowNull);
    	return ResponseInfo.successInfo("更新[非空]属性成功！");
    }
    
    /**
     * 更新“非空”属性
     * @param fieldId
     * @param allowNull
     * @return
     * @date 2018年5月30日上午8:41:39
     */
    @RequestMapping("update/isPrimaryKey/{fieldId}/{isPrimaryKey}")
    @ResponseBody
    public ResponseInfo updateIsPrimaryKey(@PathVariable("fieldId") Long fieldId, @PathVariable("isPrimaryKey") String isPrimaryKey) {
    	tableFieldService.updateIsPrimaryKey(fieldId, isPrimaryKey);
    	return ResponseInfo.successInfo("更新[主键]属性成功！");
    }
    
    /**
     * 更新“display”属性
     * @param fieldId
     * @param display
     * @return
     * @date 2018年5月30日上午8:41:39
     */
    @RequestMapping("update/display/{fieldId}")
    @ResponseBody
    public ResponseInfo updateDisplay(@PathVariable("fieldId") Long fieldId, String display) {
    	tableFieldService.updateDisplay(fieldId, display);
    	return ResponseInfo.successInfo("更新[display]属性成功！");
    }
    /**
     * 更新注释属性
     * @param fieldId
     * @param comment
     * @return
     * @author iuie
     * @date 2018年5月30日 下午12:02:53
     */
    @RequestMapping("update/comment/{fieldId}")
    @ResponseBody
    public ResponseInfo updateComment(@PathVariable("fieldId") Long fieldId, String comment) {
    	tableFieldService.updateComment(fieldId, comment);
    	return ResponseInfo.successInfo("更新[注释]属性成功！");
    }
    
    /**
     * 根据fieldId查询单条字段信息
     * @param fieldId
     * @return
     */
    @RequestMapping("find/field/id/{fieldId}")
    @ResponseBody
    public ResponseInfo findFieldById(@PathVariable("fieldId") Long fieldId) {
    	TableFieldVO field = tableFieldService.findById(fieldId);
    	return ResponseInfo.successInfoByObject(field);
    }
    
    /**
     * 根据tableId分页查询变更记录
     * @param tableId
     * @param page
     * @return
     */
    @RequestMapping("find/changeHistory/tableId/{tableId}")
    @ResponseBody
    public ResponseInfo findChangeHistoryByPage(@PathVariable("tableId") Long tableId, Page page) {
    	List<TableChangeHistoryVO> list = tableStructureService.findChangeHistoryByPage(tableId, page);
    	return ResponseInfo.successInfo(list, page);
    }
    
    /**
     * 转JavaBean
     * @param header
     * @param tableId
     * @return
     */
    @RequestMapping("transTo/javaBean/{tableId}")
    @ResponseBody
    public ResponseInfo transToJavaBean(JavaBeanDevHeaderTO header, @PathVariable("tableId") Long tableId) {
    	DevTransferResultVO result = transferService.transToJavaBean(header, tableId);
    	return ResponseInfo.successInfo("转换成功！", result);
    }
    
    /**
     * 转ResultMap
     * @param header
     * @param tableId
     * @return
     */
    @RequestMapping("transTo/resultMap/{tableId}")
    @ResponseBody
    public ResponseInfo transToResultMap(ResultMapDevHeaderTO header, @PathVariable("tableId") Long tableId) {
    	DevTransferResultVO result = transferService.transToResultMap(header, tableId);
    	return ResponseInfo.successInfo("转换成功！", result);
    }
    
    /**
     * 转JavaMapper
     * @param header
     * @param tableId
     * @return
     */
    @RequestMapping("transTo/javaMapper/{tableId}")
    @ResponseBody
    public ResponseInfo transToJavaMapper(ResultMapDevHeaderTO header, @PathVariable("tableId") Long tableId) {
    	DevTransferResultVO result = transferService.transToJavaMapper(header, tableId);
    	return ResponseInfo.successInfo("转换成功！", result);
    }
    
    /**
     * 转DataSet
     * @param header
     * @param tableId
     * @return
     */
    @RequestMapping("transTo/dataSet/{tableId}")
    @ResponseBody
    public ResponseInfo transToDataSet(DataSetDevHeaderTO header, @PathVariable("tableId") Long tableId) {
    	DevTransferResultVO result = transferService.transToDataSet(header, tableId);
    	return ResponseInfo.successInfo("转换成功！", result);
    }
    
    /**
     * 转表创建的SQL
     * @param tableId
     * @return
     * @date 2018年4月25日上午11:29:44
     */
    @RequestMapping("transTo/tableCreateSql/{tableId}")
    @ResponseBody
    public ResponseInfo transToTableCreateSql(SQLDevHeaderTO header, @PathVariable("tableId") Long tableId) {
    	DevTransferResultVO result = transferService.transToTableCreateSql(header, tableId);
    	return ResponseInfo.successInfo("转换成功！", result);
    }
}
